/**
 * 
 * @author  phoenics@126.com
 * @version v.0.1
 * @date  2018年4月30日 下午5:56:02 
 * 
 */
package phoenics.upservice.restcontroller;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import phoenics.common.BaseData.RestResp;
import phoenics.upservice.auth.JwtAuthentication;
import phoenics.upservice.dto.ListPage;
import phoenics.upservice.dto.ListTerm;
import phoenics.upservice.dto.ProjectDTO;
import phoenics.upservice.dto.ProjectPicDTO;
import phoenics.upservice.entity.Project;
import phoenics.upservice.entity.User;
import phoenics.upservice.entity.UserProject;
import phoenics.upservice.repository.ProjectRepo;
import phoenics.upservice.repository.ProjectRepo2;
import phoenics.upservice.repository.UserProjectRepository;
import phoenics.upservice.service.ProjectServiceBack;
import phoenics.upservice.service.UserService;
import phoenics.upservice.service.UtilSerice;

/**
 *
 */
@Api(value = "与工程信息相关API")
@RestController  
@RequestMapping("/myinfo")  
public class MyInfo {
	private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(MyInfo.class);
	private SimpleDateFormat myFmt=new SimpleDateFormat("yyyy-MM-dd");
    private SimpleDateFormat myFmt2=new SimpleDateFormat("yyyy年MM月dd日");
    @Autowired
	ProjectRepo projectRepo;
	@Autowired
	UserProjectRepository userProjectRepository;
	@Autowired
	UserService userService;
	@Autowired
	UtilSerice utilSerice;
	 @ApiOperation(value = "我自己发布的信息", notes = "")  
	@GetMapping  (value = "/publish")
	 public List<ListPage> projectPublishs() {
		 User userE=userService.takePrincipal();
		 List<Project> lls=projectRepo.findByUserId(userE.getId());
		 List<ListPage> content=lls.stream().map(u->utilSerice.ListPageMapper(u)).collect(Collectors.toList());
		 return content;
	 }
   
    @ApiOperation(value = "成交工程信息列表", notes = "")  
	@GetMapping  (value = "/list")
	public RestResp projectInfos(
			) {
		JwtAuthentication j=(JwtAuthentication)SecurityContextHolder.getContext().getAuthentication();
		if(j==null) {
			return RestResp.fail("未登录");
		}
		User userE=(User)j.getPrincipal();
		List<UserProject> ups=userProjectRepository.closedList(userE.getId());
		List<ListPage> content=Optional.ofNullable(ups).orElseGet(Collections::emptyList).stream().map(u->utilSerice.ListPageMapper(u.getProject())).collect(Collectors.toList());
		return RestResp.success(content);
	}
    @ApiOperation(value = "成交工程信息详情", notes = "")  
   	@GetMapping  (value = "/list/{id}")
   	public RestResp projectInfo(@PathVariable("id") Long id
   			) {
   		JwtAuthentication j=(JwtAuthentication)SecurityContextHolder.getContext().getAuthentication();
   		if(j==null) {
   			return RestResp.fail("未登录");
   		}
   		User userE=(User)j.getPrincipal();
   		Optional<UserProject> ups=userProjectRepository.closedOrder(userE.getId(),id);
   		 //ups.map(u->ListPageMapper(u.getProject()));
   		 return RestResp.success(ups.map(u->pDetails(u.getProject())).orElse(null));
   	}
    
    
    
    private ProjectDTO pDetails(Project project) {
		JwtAuthentication j=(JwtAuthentication)SecurityContextHolder.getContext().getAuthentication();
		if(j==null) {
			return null;
		}
		User userE=(User)j.getPrincipal();
		ProjectDTO projectDTO=new ProjectDTO();
		//Project project=projectRepo.findOne(id);
		BeanUtils.copyProperties(project,projectDTO);
		projectDTO.setOpenDate(myFmt2.format(project.getOpenDate()));
		Optional<ProjectDTO> parentVal = Optional.ofNullable(projectDTO);
		List<ProjectPicDTO> pdto=parentVal.map(p->p.getProjectPics()).orElse(Collections.emptyList()).stream().map(p->{
			ProjectPicDTO dto=new ProjectPicDTO();
			dto.setDetail(p.getDetail());
			dto.setPicUrl(p.getPicUrl());
			return dto;
		}).collect(Collectors.toList());
		project.getUsers().stream().filter(u->u.getId().equals(takeThisUser().get().getId())).findFirst().ifPresent(u->projectDTO.setConcern(true));
		projectDTO.setProjectPics(pdto);
		return projectDTO;
	}
	
   
	private Optional<User> takeThisUser(){
		JwtAuthentication j=(JwtAuthentication)SecurityContextHolder.getContext().getAuthentication();
		Optional<JwtAuthentication> optional_j = Optional.ofNullable(j);
		return optional_j.map(jop->(User)jop.getPrincipal());
		
	}
	
	
	
	
}
